//
//  NavigationBar+Background.m
//  LotteryClient
//
//  Created by Suny on 14-7-7.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#import "NavigationBar+Background.h"
#import "UIImage+BaseFramework.h"

@implementation NavigationBar (Background)

- (void)useCustomBackgroundImage
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_7_0))
    {

    }
    else
    {
        self.barStyle        = UIBarStyleBlackOpaque;
        self.translucent     = NO;
        self.backgroundColor = [UIColor clearColor];
        self.tag             = kNavigationBarBackgroundImageTag;
        [self setNeedsDisplay];
    }
	
	_shadowImageView.hidden = NO;

}

- (void)useCustomBackgroundWithColor:(UIColor *)color
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_7_0))
    {
		self.backgroundImage =  [UIImage createImageWithColor:color];
		self.tag             = kNavigationBarBackgroundImageTag;

		[self setNeedsDisplay];

    }
    else
    {
        self.barStyle        = UIBarStyleBlackOpaque;
        self.translucent     = NO;
        self.tag             = kNavigationBarBackgroundImageTag;
        [self setNeedsDisplay];
    }

    self.backgroundColor = color;

	_shadowImageView.hidden = NO;

}

- (void)useClearColorBackground
{
	[self useCustomBackgroundWithColor:[UIColor clearColor]];
	_shadowImageView.hidden = YES;
}

- (void)useTranslucentBackgroundImage
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_7_0))
    {
        [self setBackgroundImage:nil
                   forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        self.barStyle        = UIBarStyleBlackOpaque;
        self.translucent     = NO;
        self.backgroundColor = [UIColor clearColor];
        self.tag             = kNavigationBarTranslucentBackgroundImageTag;
        [self setNeedsDisplay];
    }
	_shadowImageView.hidden = NO;


}

- (void)useBlackTranslucentBackgroundImage
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_7_0))
    {
        [self setBackgroundImage:[UIImage createImageWithColor:[UIColor colorWithRed:0
                                                                               green:0
                                                                                blue:0
                                                                               alpha:0.65]]
                   forBarMetrics:UIBarMetricsDefault];

    }
    else
    {

        self.barStyle        = UIBarStyleBlackTranslucent;
        self.translucent     = YES;
        self.backgroundColor = [UIColor clearColor];
        self.tag             = kNavigationBarBlackTranslucentBackgroundImageTag;
        [self setNeedsDisplay];
    }
	_shadowImageView.hidden = NO;

}

- (void)useWhiteTranslucentBackgroundImage
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_7_0))
    {
        [self setBackgroundImage:[UIImage whiteGradiantImageWithSize:self.frame.size]
                   forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        self.barStyle        = UIBarStyleBlackOpaque;
        self.translucent     = NO;
        self.backgroundColor = [UIColor clearColor];
        self.tag             = kNavigationBarWhiteTranslucentBackgroundImageTag;
        [self setNeedsDisplay];
    }
	_shadowImageView.hidden = NO;

}

- (void)drawRect:(CGRect)rect
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_7_0))
    {
        [super drawRect:rect];
		self.backgroundColor = [UIColor clearColor];
		[self.backgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

    }
    else
    {
        self.backgroundColor = [UIColor clearColor];
        UIImage *backgroundImage = nil;
        if (self.tag == kNavigationBarBlackTranslucentBackgroundImageTag)
        {
            backgroundImage = [UIImage createImageWithColor:[UIColor colorWithRed:0
                                                                            green:0
                                                                             blue:0
                                                                            alpha:0.65]];
        }
        else if (self.tag == kNavigationBarBackgroundImageTag)
        {
            backgroundImage = [UIImage createImageWithColor:[UIColor colorWithRed:249.0 / 255.0
                                                                            green:248.0 / 255.0
                                                                             blue:249.0 / 255.0
                                                                            alpha:1]];
        }
        else if (self.tag == kNavigationBarTranslucentBackgroundImageTag)
        {
            backgroundImage = [UIImage createImageWithColor:[UIColor colorWithRed:249.0 / 255.0
                                                                            green:248.0 / 255.0
                                                                             blue:249.0 / 255.0
                                                                            alpha:0.98]];
        }
        else if (self.tag == kNavigationBarWhiteTranslucentBackgroundImageTag)
        {
            backgroundImage = [UIImage whiteGradiantImageWithSize:self.frame.size];
        }

        if (backgroundImage)
        {
            [backgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        }
    }
}

@end